﻿Imports System
Imports System.IO
Imports System.Reflection

Namespace System.Workflow.ComponentModel.Compiler
    Public NotInheritable Class WorkflowCompiler
        ' Methods
        Public Function Compile(ByVal parameters As WorkflowCompilerParameters, ByVal ParamArray files As String()) As WorkflowCompilerResults
            Dim results2 As WorkflowCompilerResults
            If (parameters Is Nothing) Then
                Throw New ArgumentNullException("parameters")
            End If
            If (files Is Nothing) Then
                Throw New ArgumentNullException("files")
            End If
            Dim domain As AppDomain = AppDomain.CreateDomain("CompilerDomain", Nothing, AppDomain.CurrentDomain.SetupInformation)
            Dim flag As Boolean = False
            Dim outputAssembly As String = parameters.OutputAssembly
            Try 
                Dim internal2 As WorkflowCompilerInternal
                If parameters.GenerateInMemory Then
                    flag = True
                    parameters.GenerateInMemory = False
                    If Not String.IsNullOrEmpty(parameters.OutputAssembly) Then
                        goto Label_0071
                    End If
                    parameters.OutputAssembly = (Path.GetTempFileName & ".dll")
                End If
                goto Label_00B1
            Label_0071:
                Try 
                    Dim info As DirectoryInfo = Directory.CreateDirectory((Path.GetTempPath & "\" & Guid.NewGuid))
                    parameters.OutputAssembly = (info.FullName & "\" & parameters.OutputAssembly)
                Catch obj1 As Object
                    goto Label_0071
                End Try
            Label_00B1:
                internal2 = DirectCast(domain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly.FullName, GetType(WorkflowCompilerInternal).FullName), WorkflowCompilerInternal)
                Dim results As WorkflowCompilerResults = internal2.Compile(parameters, files)
                If (flag AndAlso Not results.Errors.HasErrors) Then
                    results.CompiledAssembly = Assembly.Load(File.ReadAllBytes(results.PathToAssembly))
                    results.PathToAssembly = Nothing
                    Try 
                        File.Delete(parameters.OutputAssembly)
                        Directory.Delete(Path.GetDirectoryName(parameters.OutputAssembly))
                    Catch obj2 As Object
                    End Try
                End If
                results2 = results
            Finally
                Dim path As String = parameters.OutputAssembly
                If flag Then
                    parameters.GenerateInMemory = True
                    parameters.OutputAssembly = outputAssembly
                End If
                AppDomain.Unload(domain)
                If flag Then
                    Try 
                        File.Delete(path)
                    Catch obj3 As Object
                    End Try
                End If
            End Try
            Return results2
        End Function

    End Class
End Namespace

